<?php

class Admin extends Controller {

    function Admin()
    {
        parent::Controller();
        $this->load->library('form_validation');
        $this->load->model('admin_model');
        $this->output->enable_profiler(TRUE);
    }

    function index()
    {
        // If the user is not logged in as an admin nor logged in as a member,
        // go to login page
        if (!$this->auth->admin_logged_in())
        {
            redirect(base_url() . 'admin/login');
        }

        // Below this is the main admin homepage
        $this->template->build('admin/home', '', '', '', $this->config->item('site_admin_template'));
    }

    function no_permission()
    {
        $data['message'] = 'You have no permisssion to access this page';
        $this->template->build('blank', $data);
    }

    function login()
    {
        if ($this->auth->admin_logged_in())
        {
            redirect(base_url() . 'admin');
        }
        elseif ($this->auth->logged_in() && !$this->auth->admin_logged_in())
        {
            $data['heading'] = 'No permission';
            $data['message'] = 'Only the site\'s administrators can access this page';
            $this->template->build('users/message', $data);
        }
        else
        {
            $this->load->library('form_validation');

            // Set rules for the login form
            $this->form_validation->set_rules('email', 'email', 'trim|required|max_length[50]|valid_email');
            $this->form_validation->set_rules('password', 'password', 'required|min_length[6]|max_length[20]');

            // Check if the form validation was passed
            if ($this->form_validation->run() == FALSE)
            {
                // this determines the status messages if a login was previously
                // submitted
                $status = $this->session->flashdata('status');

                $data['status_message'] = ''; // defines status message
                if ($status == 'NOT_ACTIVATED')
                {
                    // grab the flashed data from previously submitted logins
                    $masked_user_id = $this->session->flashdata('masked_user_id');
                    $not_activated_key = $this->session->flashdata('not_activated_key');

                    // generate link to email activation key again
                    $email_activation_link = base_url() . 'users/email_activation/' . $masked_user_id . '/' . $not_activated_key;

                    // set the status message
                    $data['status_message'] = 'Your email has not been activated.
						If you want to resend your activation email, click <a href="' . $email_activation_link . '">here</a>.';
                }
                elseif ($status == 'ACTIVATION_EMAIL_SENT')
                {
                    $data['status_message'] = 'Your activation email has been resent.';
                }
                elseif ($status == 'BANNED')
                {
                    $data['status_message'] = $this->session->flashdata('reason');
                }
                elseif ($status == 'WRONG_EMAIL_PASSWORD')
                {
                    $data['status_message'] = 'The email or password you have entered is incorrect';
                }

                $header['title'] = 'Administrator\'s Login';
                $this->template->build('users/auth/login', $data, $header);
            }
            else
            {
                $auth = $this->auth->login($this->input->post('email'), $this->input->post('password'));

                if ($auth['status'] === 'BANNED')
                {
                    $flashdata = array(
                        'status' => $auth['status'],
                        'reason' => $auth['reason']
                    );
                    $this->session->set_flashdata($flashdata);
                    redirect(base_url() . 'admin/login');
                }
                elseif ($auth['status'] === 'NOT_ACTIVATED')
                {
                    // grabs the key and masked user ID to be used in creating
                    // the link to resend the activation email
                    $flashdata = array(
                        'status' => $auth['status'],
                        'masked_user_id' => $auth['masked_user_id'],
                        'not_activated_key' => $auth['not_activated_key']
                    );
                    $this->session->set_flashdata($flashdata);
                    redirect(base_url() . 'admin/login');
                }
                elseif ($auth['status'] === FALSE)
                {
                    $this->session->set_flashdata('status', 'WRONG_EMAIL_PASSWORD');
                    redirect(base_url() . 'admin/login');
                }
                elseif ($auth['status'] === TRUE && $this->auth->admin_logged_in())
                {
                    redirect(base_url() . 'admin');
                }
                elseif ($auth['status'] === TRUE && !$this->auth->admin_logged_in())
                {
                    $data['heading'] = 'No permission';
                    $data['message'] = 'Only the site\'s administrators can access this page';
                    $this->template->build('users/message', $data);
                }
            }
        }
    }

}